package cn.bellychang.alibaba;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * @author ChangLiang
 * @date 2021/3/18
 */
public class Solution {

    /**
     * 最长重复子串 如aabbcceee 结果 eee
     * @param str
     * @return
     */
    public String getMaxRepeatingSubString(String str){

        List<ArrayList<Character>> arrayList = new ArrayList<>();
        Deque<Character> stack = new ArrayDeque<>();

        char[] chars = str.toCharArray();

        for (char c : chars) {
            stack.push(c);
        }

        char temp = ' ';

        ArrayList<Character> list = new ArrayList<>();

        while (!stack.isEmpty()) {
            while (temp == ' ' || (stack.peek()!=null && stack.peek() == temp)) {
                temp = stack.pop();
                list.add(temp);
            }
            arrayList.add(list);
            list = new ArrayList<>();
            temp = ' ';
        }


        int maxCount = 0;
        int maxIndex = 0;
        for (int i = 0; i < arrayList.size(); i++) {
            int arraySize = arrayList.get(i).size();
            if (arraySize > maxCount) {
                maxCount = arraySize;
                maxIndex = i;
            }
        }

        ArrayList<Character> tempList = arrayList.get(maxIndex);

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < tempList.size(); i++) {
            builder.append(tempList.get(i));
        }

        return builder.toString();
    }
}
